﻿using System.Globalization;
using System.Web.Mvc;
using Castle.Core.Logging;
using VideoSeller.Business.Models;
using VideoSeller.Core.Configuration;

namespace VideoSeller.Web.Controllers
{
	public class LanguageController : BaseController
	{
		private readonly ClientModel _clientModel;

		public LanguageController(ClientModel clientModel)
		{
			_clientModel = clientModel;
		}

		public ActionResult ChangeCulture(string cultureName, string returnUrl)
		{
			try
			{
				var culture = CultureInfo.GetCultureInfo(cultureName);
				HttpContext.Profile["Culture"] = culture.Name;

				if (CurrentClient != null)
				{
					_clientModel.ChangeCulture(CurrentClient, culture.Name);
				}
			}
			catch (CultureNotFoundException exception)
			{
				var logger = IoC.Resolve<ILogger>();
				logger.Error(exception.Message, exception);
			}

			if (!string.IsNullOrEmpty(returnUrl))
			{
				return Redirect(returnUrl);
			}

			return RedirectToAction("Index", "Home");
		}
	}
}